package com.jian.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jian.bus.pojo.Goods;
import com.jian.bus.pojo.Inport;
import com.jian.bus.pojo.Provider;
import com.jian.bus.service.GoodsService;
import com.jian.bus.service.InportService;
import com.jian.bus.service.ProviderService;
import com.jian.bus.vo.InportVo;
import com.jian.bus.vo.ProviderVo;
import com.jian.sys.common.DataGridView;
import com.jian.sys.common.ResultObj;
import com.jian.sys.common.WebUtils;
import com.jian.sys.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  商品进货管理前端控制器
 * </p>
 *
 * @author 剑
 * @since 2020-01-22
 */
@RestController
@RequestMapping("/inport")
public class InportController {
    @Autowired
    InportService inportService;
    @Autowired
    GoodsService goodsService;
    @Autowired
    ProviderService providerService;
    /**
     * 查询所有和模糊查询
     */
    @RequestMapping("/loadAllInport")
    public DataGridView loadAllInport(InportVo inportVo){

        IPage<Inport> page = new Page<>(inportVo.getPage(),inportVo.getLimit());
        QueryWrapper<Inport> wrapper = new QueryWrapper<>();
        wrapper.eq(inportVo.getProviderid()!=null&&inportVo.getProviderid()!=0,"providerid",inportVo.getProviderid());
        wrapper.eq(inportVo.getGoodsid()!=null&&inportVo.getGoodsid()!=0,"goodsid",inportVo.getGoodsid());
        wrapper.like(StringUtils.isNotBlank(inportVo.getOperateperson()),"operateperson",inportVo.getOperateperson());
        wrapper.like(StringUtils.isNotBlank(inportVo.getRemark()),"remark",inportVo.getRemark());
        wrapper.ge(inportVo.getStartTime()!=null,"inporttime",inportVo.getStartTime());
        wrapper.le(inportVo.getEndTime()!=null,"inporttime",inportVo.getEndTime());
        wrapper.orderByDesc("inporttime");
        this.inportService.page(page,wrapper);
        List<Inport> list = page.getRecords();
        //通过商品id设置商品名称 商品规格
        for (Inport inport : list) {
            Goods goods = this.goodsService.getById(inport.getGoodsid());
            if(null!=goods){
                inport.setGoodsname(goods.getGoodsname());
                inport.setSize(goods.getSize());
            }
        }
        //通过供应商id设置供应商名称
        for (Inport inport : list) {
            Provider provider = this.providerService.getById(inport.getProviderid());
            if(null!=provider){
                inport.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(page.getTotal(),list);
    }
    /**
     * 添加商品进货
     * @param inportVo
     * @return
     */
    @RequestMapping("/addInport")
    public ResultObj addInport(InportVo inportVo){
        try{
            inportVo.setInporttime(new Date());
            User user= (User) WebUtils.getSession().getAttribute("user");
            inportVo.setOperateperson(user.getName());
            this.inportService.save(inportVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    /**
     * 修改商品进货
     * @param inportVo
     * @return
     */
    @RequestMapping("/updateInport")
    public ResultObj updateInport(InportVo inportVo){
        try{
            this.inportService.updateById(inportVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }
    /**
     * 删除商品进货
     * @param id
     * @return
     */
    @RequestMapping("/deleteInport")
    public ResultObj deleteInport(Integer id){
        try {
            this.inportService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}

